package cn.tedu.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;

public class HdfsDemo {

    public static void main(String[] args) throws IOException {

        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop01:8020");

        FileSystem fs = FileSystem.get(conf);
        System.out.println(fs);
    }

    /*
        创建目录
     */
    @Test
    public void mkdirPath() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop01:8020");
        FileSystem fs = FileSystem.get(conf);
        fs.mkdirs(new Path("/test/input/one"));

        fs.close();
    }

    /*
        上传文件
     */
    @Test
    public void putFiles() throws IOException{

        //获取文件系统对象
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop01:8020");
        FileSystem fs = FileSystem.get(conf);

        //上传文件
        fs.copyFromLocalFile(new Path("file:///E:/1.txt"),//本地路径
                             new Path("/test/input"));//系统路径

        fs.close();
    }
    /*
        下载文件
     */
    @Test
    public void getFiles() throws IOException{
        //获取文件系统对象
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop01:8020");
        FileSystem fs = FileSystem.get(conf);

        //获取文件
        fs.copyToLocalFile(new Path("/test/input/1.txt"), //系统路径
                           new Path("file:///E:/2.txt"));//本地路径
        fs.close();
    }
}
